<?php



/**
 * Obligations Controller
 *
 * @property Obligation $Obligation
 */
class ObligationsController extends AppController {

    public $name = "Obligations";

    /**
     * index method
     *
     * @return void
     */
    public function index($id) {
        $this->set('contract_id', $id);
        $this->Obligation->recursive = 0;
        $this->set('obligations', $this->Obligation->find('all', array('conditions' => array('Obligation.contract_id' => $id))));
    }

    /**
     * view method
     *
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->Obligation->id = $id;
        if (!$this->Obligation->exists()) {
            throw new NotFoundException(__('Invalid obligation'));
        }
        $this->set('obligation', $this->Obligation->read(null, $id));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add($contrac_id) {
        
     
        if (!empty($this->data)) {
            
            $this->data['Obligation']['contract_id']=$contrac_id;
            $this->Obligation->create();
            if ($this->Obligation->save($this->data)) {
                $this->Session->setFlash(__('Registro guardado correctamente'),'flash_custom');
                $this->redirect(array('action' => 'index',$contrac_id));
            } else {
                   $this->Session->setFlash(__('ERROR GUARDANDO DATOS'),'flash_custom');
                
            }
           
        }else{
             $this->data['Obligation']['contract_id']=$contrac_id;
        }
        
    }

    /**
     * edit method
     *
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->Obligation->id = $id;
        $this->Obligation->recursive=-1;
        $this->layout='ajax';
        if (!$this->Obligation->exists()) {
            throw new NotFoundException(__('Invalid obligation'));
        }
        if (!empty($this->data)) {
            if ($this->Obligation->save($this->data)) {
                $this->Session->setFlash(__('Registro editado correctamente'),'flash_custom');
                $this->redirect(array('action' => 'index',$this->data['Obligation']['contract_id']));
            } else {
                $this->Session->setFlash(__('Error al guardar.','flash_custom'));
            }
        } else {
            $this->data = $this->Obligation->read(null, $id);
        }
        
    }

    /**
     * delete method
     *
     * @param string $id
     * @return void
     */
    public function delete($id = null,$contract_id) {
        if (!$this->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Obligation->id = $id;
        if (!$this->Obligation->exists()) {
            throw new NotFoundException(__('Invalid obligation'));
        }
        if ($this->Obligation->delete()) {
            $this->Session->setFlash('Registro borrado','flash_custom');
            $this->redirect(array('controller'=>'Obligations', 'action' => 'index',$contract_id));
        }
        
    }

}
